<template>
  <div class="serach-user">
    <div class="userCount">找到{{userCount}}位用户</div>
    <div class="users-list">
      <div @click.stop="$router.push({ name: 'userAccount', query: { id:item.userId
 } });" v-for="(item,index) of users" :key="index+'user'" class="user">
        <img class="img" :src="item.avatarUrl+'?param=70y70'" alt="">
        <div class="name">{{item.nickname}}</div>
        <!-- 男 -->
        <div v-if="item.gender == 1" class="gender1">
          <div class="male"><span class="el-icon-male"></span></div>
          <!-- 女 -->
        </div>
        <div v-if="item.gender ==2 " class="gender2">
          <div class="female"><span class="el-icon-female"></span></div>
        </div>
        <div class="desc">{{ getDesc(item)}}</div>
      </div>
    </div>
    <div v-show=" userCount>=30" class="pageset">
      <el-pagination :page-size="30" :current-page.sync="curPage" @current-change="getsearchUser()"
        layout="prev, pager, next" :total="userCount" background></el-pagination>
    </div>
  </div>
</template>
<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getsearchAPI } from "@/api/search";
export default {
  name: "serach-user",
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {
    searchText: {
      require: true,
    },
  },
  data() {
    //这里存放数据
    return {
      users: {},
      userCount: 0,
      limit: 0,
      curPage: 1,
      pageSize: 30,
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    searchText() {
      this.getsearchUser();
    },
  },
  //方法集合
  methods: {
    getDesc(user) {
      if (user.description != "") {
        return user.description;
      } else {
        return user.signature;
      }
    },
    async getsearchUser() {
      try {
        this.users = [];
        const { data } = await getsearchAPI(
          this.searchText,
          30,
          (this.curPage - 1) * 30,
          1002
        );
        // console.log(data.result);
        this.users = data.result.userprofiles;
        this.userCount = data.result.userprofileCount;
        // 页面滚动优化用户体验
        if (document.documentElement.scrollTop != 0) {
          document.documentElement.scrollTop = 0;
        }
      } catch (error) {
        console.log(error + "搜索歌手失败");
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getsearchUser();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang='less'>
.serach-user {
  margin: 0 auto;
  padding-bottom: 200px;
  .users-list {
    display: flex;
    flex-wrap: wrap;
    .user {
      position: relative;
      flex-shrink: 0;
      width: 1200px;
      display: flex;
      align-items: center;
      cursor: pointer;
      .img {
        margin: 10px 10px 10px 10px;
        height: 70px;
        width: 70px;
        border-radius: 70px;
      }
      .name {
        color: rgb(68, 68, 68);
      }
      .desc {
        position: absolute;
        color: rgb(167, 167, 167);
        margin-left: 10px;
        border-radius: 20px;
        right: 30px;
        max-width: 300px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
      .gender1 {
        width: 22px;
        height: 22px;
        background-color: #bbeffb;
        border-radius: 20px;
        margin-left: 10px;
        .male {
          padding-left: 3px;
          color: #64b9de;
        }
      }
      .gender2 {
        width: 22px;
        height: 22px;
        background-color: #f5c3de;
        border-radius: 20px;
        margin-left: 10px;
        .female {
          padding-left: 3px;
          color: #eb73a6;
        }
      }
    }
    .user:nth-child(even) {
      background-color: #fafafa;
    }
    .user:hover {
      background-color: #f1f2f3;
    }
  }

  .pageset {
    display: flex;
    justify-content: center;
    .el-pagination {
      margin-top: 50px;
      margin-bottom: 140px;
    }
  }
}
</style>